from PyQt4.QtCore import *
from PyQt4.QtGui import *

import scene

class Blob(QGraphicsRectItem):
    def __init__(self,x,y,w,h):
        QGraphicsRectItem.__init__(self,x,y,w,h)
        self.setFlag(self.ItemIsMovable)
        self.setBrush(QBrush(QColor(Qt.red)))
        self.tl = Handle(x,y,w/3.0,h/3.0,self)
        self.bl = Handle(x,y+h/2.0,w/3.0,h/3.0,self)

class Handle(QGraphicsRectItem):
    def __init__(self,x,y,w,h,parent):
        QGraphicsRectItem.__init__(self,-w/2.0,-h/2.0,w,h,parent)
        self.setPos(x,y)
        self.setBrush(QBrush(QColor(Qt.blue)))
    def mousePressEvent(self,event):
        spos1 = self.mapToScene(self.x(),self.y())
        spos=self.scenePos()
        print "my x,y: %s %s " % (self.x(),self.y())
        print "pos x,y: %s %s " % (self.pos().x(),self.pos().y())
        print "my scene coords: %s %s " % (spos.x(),spos.y())
        print "Got ",event.pos().x(),event.pos().y()
        ppos = self.parentItem().mapToScene(event.pos())
        print "Scene ",ppos.x(),ppos.y()
        print "---"
        

from layout import Ui_Dialog
class GraphDialog(QDialog,Ui_Dialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setupUi(self)

        self.scene = scene.modelScene()
        self.graphicsView.setScene(self.scene)

        b1 = Blob(0,0,30,30)
        self.scene.addItem(b1)
        
        b2 = Blob(40,0,20,20)
        self.scene.addItem(b2)
        

def main(args):
    app = QApplication(args)
    gd=GraphDialog()
    gd.show()
    app.exec_()

    
import sys
if __name__=="__main__":
    main(sys.argv)
